﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using Model;

namespace OAS
{
    public partial class UserInfoUpdate : System.Web.UI.Page
    {
        UserInfoBLL userInfoBLL = new UserInfoBLL();
        public int UserIdQueryString
        {
            get
            {
                int _UserId = 0;
                if (Request.QueryString["id"] != null)
                {
                    _UserId = Convert.ToInt32(Request.QueryString["id"]);
                }
                return _UserId;
            }
        }
        /// <summary>

        /// 绑定数据

        /// </summary>

        private void BindUserInfo()
        {
            var User = userInfoBLL.GetUserInfo(UserIdQueryString);
            UserAccountTextBox.Text = User.UserAccount;
            UserNameTextBox.Text = User.UserName;
            TitleTextBox.Text = User.Title;
            DomainTextBox.Text = User.Domain;
            IsActiveCheckBox.Checked = User.IsActive;
            EmailTextBox.Text = User.Email;
            foreach (SecurityUserRoleMappingEntity userRole in userInfoBLL.GetUserRoleMappingList(UserIdQueryString))
            {
                for (int i = 0; i < chklRole.Items.Count; i++)
                {
                    if (userRole.RoleId == Convert.ToInt32(chklRole.Items[i].Value)) chklRole.Items[i].Selected = true;
                }
            }
            foreach (SecurityUserFactoryMappingEntity userFactory in userInfoBLL.GetUserFactoryMappingList(UserIdQueryString))
            {
                for (int i = 0; i < chklFactory.Items.Count; i++)
                {
                    if (userFactory.FactoryCode == chklFactory.Items[i].Value) chklFactory.Items[i].Selected = true;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定数据

                if (UserIdQueryString != 0)
                {
                    BindUserInfo();
                }

            }
        }
        /// <summary>

        /// 保存事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //添加用户

                var _NewUserId = userInfoBLL.UpdateUser(GetInputUser());
                //添加用户角色

                userInfoBLL.UpdateUserRole(GetInputUserRole(_NewUserId),UserIdQueryString);
                //添加用户工厂

                userInfoBLL.UpdateUserFactory(GetInputUserFactory(_NewUserId),UserIdQueryString);

            }
        }
        /// <summary>

        /// 用户信息

        /// </summary>

        /// <returns></returns>

        public SecurityUserEntity GetInputUser()
        {
            SecurityUserEntity _User = null;
            if (UserIdQueryString == 0)
            {
                _User = new SecurityUserEntity();
                _User.Password = "N8Xyp+oJG03e0Zk0L3q0109+F7coc6MYtc48mlaXSkSZFgaZdAOyH8shRQfyTY7i6Tx4RkGBffEqOVY4/ciHgHzim9pMBDnO2M6Hm0xGDVs=";
            }
            else
            {
                _User = userInfoBLL.GetUserInfo(UserIdQueryString);
            }
            _User.Id = UserIdQueryString;
            _User.UserAccount = UserAccountTextBox.Text.Trim();
            _User.UserName = UserNameTextBox.Text.Trim();
            _User.Title = TitleTextBox.Text.Trim();
            _User.Domain = DomainTextBox.Text;
            _User.ExpireDate = DateTime.Now;
            _User.IsActive = IsActiveCheckBox.Checked;
            _User.Email = EmailTextBox.Text;
            
            return _User;
        }
        /// <summary>

        ///  用户角色列表

        /// </summary>

        /// <param name="_NewUserId">新增用户Id</param>

        public List<SecurityUserRoleMappingEntity> GetInputUserRole(int _NewUserId)
        {
            List<SecurityUserRoleMappingEntity> _UserRoleList = new List<SecurityUserRoleMappingEntity>();
            for (int i = 0; i < chklRole.Items.Count; i++)
            {
                if (chklRole.Items[i].Selected)
                {
                    SecurityUserRoleMappingEntity _UserRole = new SecurityUserRoleMappingEntity();
                    _UserRole.UserId = _NewUserId;
                    _UserRole.RoleId = Convert.ToInt32(chklRole.Items[i].Value);
                    _UserRole.IsDefaultRole = true;
                    _UserRoleList.Add(_UserRole);
                }
            }
            return _UserRoleList;
        }

        /// <summary>

        ///  用户工厂列表

        /// </summary>

        /// <param name="_NewUserId">新增用户Id</param>

        public List<SecurityUserFactoryMappingEntity> GetInputUserFactory(int _NewUserId)
        {
            List<SecurityUserFactoryMappingEntity> _UserFactoryList = new List<SecurityUserFactoryMappingEntity>();
            for (int i = 0; i < chklFactory.Items.Count; i++)
            {
                if (chklFactory.Items[i].Selected)
                {
                    SecurityUserFactoryMappingEntity _UserFactory = new SecurityUserFactoryMappingEntity();
                    _UserFactory.UserId = _NewUserId;
                    _UserFactory.FactoryCode = chklFactory.Items[i].Value;
                    _UserFactory.IsDefaultFactory = true;
                    _UserFactoryList.Add(_UserFactory);
                }
            }
            return _UserFactoryList;
        }
    }
}
